package a09_JUC;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author quan
 * @Description CountDownLatch
 * @Date 2023/5/18 16:33
 */
public class a06_CountDownLatch {
    //1、定义一个CountDownLatch
    private static CountDownLatch countDownLatch = new CountDownLatch(10);
    public static void main(String[] args) throws InterruptedException {
        System.out.println("所有选手已经就位...");
        //2、创建线程模拟跑步比赛
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(()->{
                //3、10个线程开始出发
                System.out.println(Thread.currentThread().getName()+"出发");
                try{
                    //4、等待5s开始陆续到达终点
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //5、每到达一个线程，计数-1
                System.out.println(Thread.currentThread().getName()+"到达终点");
                countDownLatch.countDown();
            },"player"+(i+1));
            //6、启动线程
            thread.start();
        }
        //7、等待所有的线程都到达终点后，开始执行后面的任务
        countDownLatch.await();
        //后面的任务就是颁奖啦~
        System.out.println("颁奖中...");
    }
}
